﻿using System;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform.Kq;
using Hjt212Parser;

namespace Biz.Process
{
    /// <summary>
    /// 负责处理响应指令：9011、9012、9014
    /// </summary>
    public static class ProcessorResponse
    {
        /// <summary>
        /// 未处理：0；
        /// 正在下行：1；
        /// 下行成功：2；
        /// 下行失败：3；
        /// 执行成功：4；
        /// 执行失败：5.
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process9011(PlatformYgContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            //var qnRtn = Parser.GetStringByKey("QnRtn", content);
            var transmit = pCtx.T_transmittal.Where(t => t.QN.Equals(qn)).FirstOrDefault(t => t.MN.Equals(mn));
            if (transmit != null)
            {
                transmit.Flag_process = "2";
                transmit.Reason = "下行成功";
                pCtx.SaveChanges();
            }
            var result = pCtx.T_transmittal_Exec_Result.Where(t => t.QN.Equals(qn)).FirstOrDefault(t => t.MN.Equals(mn));
            if (result != null)
            {
                result.Flag_process = "2";
                result.Reason = "下行成功";
                result.Flag_Alert = "0";
                pCtx.SaveChanges();
            }
            var valve = pCtx.Envi_PS_Close_Valve_Setting
                .Where(t => t.QN.Equals(qn)).FirstOrDefault(t => t.Let_Port_ID == port.ID);
            if (valve != null)
            {
                valve.Flag = "2";
                
                pCtx.SaveChanges();
            }
            var plan = pCtx.Envi_Transmittal_Plan.Where(t => t.QN.Equals(qn))
                .Where(t=>t.CN.Equals("3093"))
                .OrderByDescending(t=>t.StartTime)
                .FirstOrDefault(t => t.MN.Equals(mn));
            if (plan != null)
            {
                plan.Flag_process = "2";
                plan.Reason = "下行成功";
                pCtx.SaveChanges();
            }
        }
        public static void Process9012(PlatformYgContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            var exeRtn = Parser.GetStringByKey("ExeRtn", content);
            var transmit = pCtx.T_transmittal.Where(t => t.QN.Equals(qn)).FirstOrDefault(t => t.MN.Equals(mn));
            if (transmit != null)
            {
                transmit.Flag_process = "4";
                if (exeRtn.Equals("1"))
                {
                    transmit.Reason = "操作成功";
                }
                else if (exeRtn.Equals("2"))
                {
                    transmit.Reason = "执行失败";
                }
                else if (exeRtn.Equals("3"))
                {
                    transmit.Reason = "提取时无阀信号";
                }
                else
                {
                    transmit.Reason = "未知原因";
                }
                pCtx.SaveChanges();
            }
            var result = pCtx.T_transmittal_Exec_Result
                .Where(t => t.QN.Equals(qn)).FirstOrDefault(t => t.MN.Equals(mn));
            if (result != null)
            {
                if (exeRtn.Equals("1"))
                {
                    result.Reason = "操作成功";
                    result.Flag_process = "4";
                }
                else if (exeRtn.Equals("2"))
                {
                    result.Reason = "执行失败";
                    result.Flag_process = "5";
                }
                else if (exeRtn.Equals("3"))
                {
                    result.Reason = "提取时无阀信号";
                    result.Flag_process = "4";
                }
                else
                {
                    result.Reason = "未知原因";
                    result.Flag_process = "4";
                }
            }
            pCtx.SaveChanges();
            //更新放口充值卡总额和余额
            var price = pCtx.T_price.FirstOrDefault(t => t.Type.Equals("污水"));
            transmit = pCtx.T_transmittal.Where(t => t.QN.Equals(qn)).Where(t => t.CN.Equals("3091")).FirstOrDefault(t => t.MN.Equals(mn));
            var card = pCtx.Envi_PS_Let_Port_Recharge_Card.FirstOrDefault(t => t.Let_Port.Equals(port.ID + ""));
            if (transmit != null && card != null)
            {
                var charge = Parser.GetDecimalByKey("Charge", transmit.CP);
                card.Water += charge;
                card.Total_Amount += charge * price?.price;
                card.Remain_Amount += charge * price?.price;
                card.COD += Decimal.Parse(transmit.PNO);
                card.AN_Total += Decimal.Parse(transmit.PNUM);
                card.CardCount += 1;
                pCtx.SaveChanges();
                //todo:添加远程充值记录中数值不对
                var record = new Envi_PS_Let_Port_Recharge_Record
                {
                    Card_ID = card.ID,
                    Recharge_Time = Parser.GetDateTimeByDataTimeThenQnThenNow(content),
                    Recharge_Amount = charge * price?.price,
                    Recharge_Water_Measure = charge,
                    Recharge_Water_Price = price?.price,
                    Recharge_Type = "远程充值",
                    Remark = "",
                    State_Flag = "",
                    Recharge_COD_Total = 0,
                    Recharge_AN_Total = 0,
                    StartTime = DateTime.Now.ToString("yyyyMMddHHmmss"),
                    endTime = DateTime.Now.ToString("yyyyMMddHHmmss")

                };
                pCtx.Envi_PS_Let_Port_Recharge_Record.Add(record);
                pCtx.SaveChanges();
            }
            var plan = pCtx.Envi_Transmittal_Plan.Where(t => t.QN.Equals(qn))
             .Where(t => t.CN.Equals("3093"))
             .OrderByDescending(t => t.StartTime)
             .FirstOrDefault(t => t.MN.Equals(mn));
            if (plan != null)
            {
                if (exeRtn.Equals("1"))
                {
                    plan.Reason = "操作成功";
                    plan.Flag_process = "4";
                }
                else if (exeRtn.Equals("2"))
                {
                    plan.Reason = "执行失败";
                    plan.Flag_process = "5";
                }
                else if (exeRtn.Equals("3"))
                {
                    plan.Reason = "提取时无阀信号";
                    plan.Flag_process = "4";
                }
                else
                {
                    plan.Reason = "未知原因";
                    plan.Flag_process = "4";
                }
                pCtx.SaveChanges();
            }
        }
        public static void Process9014(PlatformYgContext pCtx, string content)
        {

        }
    }
}
